package com.token.sysflow.hander;

import com.token.base.Pl;
import com.token.sql.IntegrateSql;
import com.token.sql.TableBeanPrepareSql;
import com.token.sysflow.bean.FSysFlowNoKey;
import com.token.sysflow.exception.ErrorCodeEnum;
import com.token.sysflow.exception.FlowException;

import java.sql.ResultSet;
import java.text.DecimalFormat;

/**
 * 流程单据号
 * @author chenjianxiong 2020/4/27
 */
public class FlowNo extends FSysFlowNoKey {
    private final Flow flow;
    private final TableBeanPrepareSql tSql;
    private final String appKeyId;

    /**
     *
     * @param f
     * @param flow
     * @param appKeyId
     * @throws Exception
     */
    public FlowNo(IntegrateSql f,Flow flow,String appKeyId) throws Exception {
        super(f);
        tSql = new TableBeanPrepareSql(this);
        this.appKeyId = appKeyId;
        this.flow = flow;
        if(flow == null || !flow.isHas()){
            throw new FlowException(ErrorCodeEnum.TOFLOW_NOFIND_ERROR);
        }
        if("".equals(flow.getFlowCode())){
            throw new FlowException(ErrorCodeEnum.FLOWNO_CODE1);
        }

        if("".equals(flow.getFlowCode1())){
            throw new FlowException(ErrorCodeEnum.FLOWNO_CODE2);
        }

        if("".equals(flow.getFlowCode2())){
            throw new FlowException(ErrorCodeEnum.FLOWNO_CODE3);
        }
    }

    /**
     *
     * @return
     */
    private String getFlowNoKey(){
        return flow.getFlowCode() + Pl.getCurDataTime(flow.getFlowCode1());
    }
    /**
     *
     * @return
     * @throws Exception
     */
    public String getNo() throws Exception{
        String strNo;
        long no = 0;
        StringBuilder str = new StringBuilder(30);
        String key = this.getFlowNoKey();
        DecimalFormat df = new DecimalFormat(flow.getFlowCode2());

        tSql.setSelectPrepare("SysId=? and AppKeyId=? and NOKey=?");
        tSql.setPstValue(1,Pl.getSysId());
        tSql.setPstValue(2,appKeyId);
        tSql.setPstValue(3,key);
        ResultSet rs = tSql.executeQuery();

        str.append(key);

        if(rs.next()){
            no = Math.max(rs.getLong("NOStart"), rs.getLong("NONum"));
            strNo = df.format(++no);
            str.append(strNo);
            long id = rs.getLong(this.getKeyName());
            rs.close();
            this.update(id,no);
        }else{
            rs.close();
            strNo = df.format(++no);
            str.append(strNo);
            this.insert(key);
        }

        return str.toString();


    }

    /**
     *
     * @param key
     * @throws Exception
     */
    private void insert(String key) throws Exception{
        super.setInsert();
        this.setSysId(Pl.getSysId());
        this.setNODesc("流程单号");
        this.setAppKeyId(appKeyId);
        this.setNOKey(key);
        this.setNONum(1);
        this.setNOStart(1);
        super.executeUpdate();
    }

    /**
     *
     * @param kid
     * @param num
     * @throws Exception
     */
    private void update(long kid,long num) throws Exception{
        tSql.setUpdatePrepare("NONum=?","NOId=?");
        tSql.setPstValue(1,num);
        tSql.setPstValue(2,kid);
        tSql.executeUpdate();
    }

    /**
     *
     */
    public void close(){
        tSql.close();
    }
}
